| Conditions | 51 |
| Total Lines | 341 |
| Code Lines | 218 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like search.js ➔ SearchBox often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | function convertToId(search) |
||
| 59 | function SearchBox(name, resultsPath, inFrame, label) |
||
| 60 | { |
||
| 61 | if (!name || !resultsPath) { alert("Missing parameters to SearchBox."); } |
||
| 62 | |||
| 63 | // ---------- Instance variables |
||
| 64 | this.name = name; |
||
| 65 | this.resultsPath = resultsPath; |
||
| 66 | this.keyTimeout = 0; |
||
| 67 | this.keyTimeoutLength = 500; |
||
| 68 | this.closeSelectionTimeout = 300; |
||
| 69 | this.lastSearchValue = ""; |
||
| 70 | this.lastResultsPage = ""; |
||
| 71 | this.hideTimeout = 0; |
||
| 72 | this.searchIndex = 0; |
||
| 73 | this.searchActive = false; |
||
| 74 | this.insideFrame = inFrame; |
||
| 75 | this.searchLabel = label; |
||
| 76 | |||
| 77 | // ----------- DOM Elements |
||
| 78 | |||
| 79 | this.DOMSearchField = function() |
||
| 80 | { return document.getElementById("MSearchField"); }; |
||
| 81 | |||
| 82 | this.DOMSearchSelect = function() |
||
| 83 | { return document.getElementById("MSearchSelect"); }; |
||
| 84 | |||
| 85 | this.DOMSearchSelectWindow = function() |
||
| 86 | { return document.getElementById("MSearchSelectWindow"); }; |
||
| 87 | |||
| 88 | this.DOMPopupSearchResults = function() |
||
| 89 | { return document.getElementById("MSearchResults"); }; |
||
| 90 | |||
| 91 | this.DOMPopupSearchResultsWindow = function() |
||
| 92 | { return document.getElementById("MSearchResultsWindow"); }; |
||
| 93 | |||
| 94 | this.DOMSearchClose = function() |
||
| 95 | { return document.getElementById("MSearchClose"); }; |
||
| 96 | |||
| 97 | this.DOMSearchBox = function() |
||
| 98 | { return document.getElementById("MSearchBox"); }; |
||
| 99 | |||
| 100 | // ------------ Event Handlers |
||
| 101 | |||
| 102 | // Called when focus is added or removed from the search field. |
||
| 103 | this.OnSearchFieldFocus = function(isActive) |
||
| 104 | { |
||
| 105 | this.Activate(isActive); |
||
| 106 | }; |
||
| 107 | |||
| 108 | this.OnSearchSelectShow = function() |
||
| 109 | { |
||
| 110 | var searchSelectWindow = this.DOMSearchSelectWindow(); |
||
| 111 | var searchField = this.DOMSearchSelect(); |
||
| 112 | |||
| 113 | if (this.insideFrame) |
||
| 114 | { |
||
| 115 | var left = getXPos(searchField); |
||
| 116 | var top = getYPos(searchField); |
||
| 117 | left += searchField.offsetWidth + 6; |
||
| 118 | top += searchField.offsetHeight; |
||
| 119 | |||
| 120 | // show search selection popup |
||
| 121 | searchSelectWindow.style.display='block'; |
||
| 122 | left -= searchSelectWindow.offsetWidth; |
||
| 123 | searchSelectWindow.style.left = left + 'px'; |
||
| 124 | searchSelectWindow.style.top = top + 'px'; |
||
| 125 | } |
||
| 126 | else |
||
| 127 | { |
||
| 128 | var left = getXPos(searchField); |
||
| 129 | var top = getYPos(searchField); |
||
| 130 | top += searchField.offsetHeight; |
||
| 131 | |||
| 132 | // show search selection popup |
||
| 133 | searchSelectWindow.style.display='block'; |
||
| 134 | searchSelectWindow.style.left = left + 'px'; |
||
| 135 | searchSelectWindow.style.top = top + 'px'; |
||
| 136 | } |
||
| 137 | |||
| 138 | // stop selection hide timer |
||
| 139 | if (this.hideTimeout) |
||
| 140 | { |
||
| 141 | clearTimeout(this.hideTimeout); |
||
| 142 | this.hideTimeout=0; |
||
| 143 | } |
||
| 144 | return false; // to avoid "image drag" default event |
||
| 145 | }; |
||
| 146 | |||
| 147 | this.OnSearchSelectHide = function() |
||
| 148 | { |
||
| 149 | this.hideTimeout = setTimeout(this.name +".CloseSelectionWindow()", |
||
| 150 | this.closeSelectionTimeout); |
||
| 151 | }; |
||
| 152 | |||
| 153 | // Called when the content of the search field is changed. |
||
| 154 | this.OnSearchFieldChange = function(evt) |
||
| 155 | { |
||
| 156 | if (this.keyTimeout) // kill running timer |
||
| 157 | { |
||
| 158 | clearTimeout(this.keyTimeout); |
||
| 159 | this.keyTimeout = 0; |
||
| 160 | } |
||
| 161 | |||
| 162 | var e = (evt) ? evt : window.event; // for IE |
||
| 163 | if (e.keyCode==40 || e.keyCode==13) |
||
| 164 | { |
||
| 165 | if (e.shiftKey==1) |
||
| 166 | { |
||
| 167 | this.OnSearchSelectShow(); |
||
| 168 | var win=this.DOMSearchSelectWindow(); |
||
| 169 | for (i=0;i<win.childNodes.length;i++) |
||
| 170 | { |
||
| 171 | var child = win.childNodes[i]; // get span within a |
||
| 172 | if (child.className=='SelectItem') |
||
| 173 | { |
||
| 174 | child.focus(); |
||
| 175 | return; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | return; |
||
| 179 | } |
||
| 180 | else if (window.frames.MSearchResults.searchResults) |
||
| 181 | { |
||
| 182 | var elem = window.frames.MSearchResults.searchResults.NavNext(0); |
||
| 183 | if (elem) elem.focus(); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | else if (e.keyCode==27) // Escape out of the search field |
||
| 187 | { |
||
| 188 | this.DOMSearchField().blur(); |
||
| 189 | this.DOMPopupSearchResultsWindow().style.display = 'none'; |
||
| 190 | this.DOMSearchClose().style.display = 'none'; |
||
| 191 | this.lastSearchValue = ''; |
||
| 192 | this.Activate(false); |
||
| 193 | return; |
||
| 194 | } |
||
| 195 | |||
| 196 | // strip whitespaces |
||
| 197 | var searchValue = this.DOMSearchField().value.replace(/ +/g, ""); |
||
| 198 | |||
| 199 | if (searchValue != this.lastSearchValue) // search value has changed |
||
| 200 | { |
||
| 201 | if (searchValue != "") // non-empty search |
||
| 202 | { |
||
| 203 | // set timer for search update |
||
| 204 | this.keyTimeout = setTimeout(this.name + '.Search()', |
||
| 205 | this.keyTimeoutLength); |
||
| 206 | } |
||
| 207 | else // empty search field |
||
| 208 | { |
||
| 209 | this.DOMPopupSearchResultsWindow().style.display = 'none'; |
||
| 210 | this.DOMSearchClose().style.display = 'none'; |
||
| 211 | this.lastSearchValue = ''; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | }; |
||
| 215 | |||
| 216 | this.SelectItemCount = function(id) |
||
| 217 | { |
||
| 218 | var count=0; |
||
| 219 | var win=this.DOMSearchSelectWindow(); |
||
| 220 | for (i=0;i<win.childNodes.length;i++) |
||
| 221 | { |
||
| 222 | var child = win.childNodes[i]; // get span within a |
||
| 223 | if (child.className=='SelectItem') |
||
| 224 | { |
||
| 225 | count++; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | return count; |
||
| 229 | }; |
||
| 230 | |||
| 231 | this.SelectItemSet = function(id) |
||
| 232 | { |
||
| 233 | var i,j=0; |
||
| 234 | var win=this.DOMSearchSelectWindow(); |
||
| 235 | for (i=0;i<win.childNodes.length;i++) |
||
| 236 | { |
||
| 237 | var child = win.childNodes[i]; // get span within a |
||
| 238 | if (child.className=='SelectItem') |
||
| 239 | { |
||
| 240 | var node = child.firstChild; |
||
| 241 | if (j==id) |
||
| 242 | { |
||
| 243 | node.innerHTML='•'; |
||
| 244 | } |
||
| 245 | else |
||
| 246 | { |
||
| 247 | node.innerHTML=' '; |
||
| 248 | } |
||
| 249 | j++; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | }; |
||
| 253 | |||
| 254 | // Called when an search filter selection is made. |
||
| 255 | // set item with index id as the active item |
||
| 256 | this.OnSelectItem = function(id) |
||
| 257 | { |
||
| 258 | this.searchIndex = id; |
||
| 259 | this.SelectItemSet(id); |
||
| 260 | var searchValue = this.DOMSearchField().value.replace(/ +/g, ""); |
||
| 261 | if (searchValue!="" && this.searchActive) // something was found -> do a search |
||
| 262 | { |
||
| 263 | this.Search(); |
||
| 264 | } |
||
| 265 | }; |
||
| 266 | |||
| 267 | this.OnSearchSelectKey = function(evt) |
||
| 268 | { |
||
| 269 | var e = (evt) ? evt : window.event; // for IE |
||
| 270 | if (e.keyCode==40 && this.searchIndex<this.SelectItemCount()) // Down |
||
| 271 | { |
||
| 272 | this.searchIndex++; |
||
| 273 | this.OnSelectItem(this.searchIndex); |
||
| 274 | } |
||
| 275 | else if (e.keyCode==38 && this.searchIndex>0) // Up |
||
| 276 | { |
||
| 277 | this.searchIndex--; |
||
| 278 | this.OnSelectItem(this.searchIndex); |
||
| 279 | } |
||
| 280 | else if (e.keyCode==13 || e.keyCode==27) |
||
| 281 | { |
||
| 282 | this.OnSelectItem(this.searchIndex); |
||
| 283 | this.CloseSelectionWindow(); |
||
| 284 | this.DOMSearchField().focus(); |
||
| 285 | } |
||
| 286 | return false; |
||
| 287 | }; |
||
| 288 | |||
| 289 | // --------- Actions |
||
| 290 | |||
| 291 | // Closes the results window. |
||
| 292 | this.CloseResultsWindow = function() |
||
| 293 | { |
||
| 294 | this.DOMPopupSearchResultsWindow().style.display = 'none'; |
||
| 295 | this.DOMSearchClose().style.display = 'none'; |
||
| 296 | this.Activate(false); |
||
| 297 | }; |
||
| 298 | |||
| 299 | this.CloseSelectionWindow = function() |
||
| 300 | { |
||
| 301 | this.DOMSearchSelectWindow().style.display = 'none'; |
||
| 302 | }; |
||
| 303 | |||
| 304 | // Performs a search. |
||
| 305 | this.Search = function() |
||
| 306 | { |
||
| 307 | this.keyTimeout = 0; |
||
| 308 | |||
| 309 | // strip leading whitespace |
||
| 310 | var searchValue = this.DOMSearchField().value.replace(/^ +/, ""); |
||
| 311 | |||
| 312 | var code = searchValue.toLowerCase().charCodeAt(0); |
||
| 313 | var idxChar = searchValue.substr(0, 1).toLowerCase(); |
||
| 314 | if ( 0xD800 <= code && code <= 0xDBFF && searchValue > 1) // surrogate pair |
||
| 315 | { |
||
| 316 | idxChar = searchValue.substr(0, 2); |
||
| 317 | } |
||
| 318 | |||
| 319 | var resultsPage; |
||
| 320 | var resultsPageWithSearch; |
||
| 321 | var hasResultsPage; |
||
| 322 | |||
| 323 | var idx = indexSectionsWithContent[this.searchIndex].indexOf(idxChar); |
||
| 324 | if (idx!=-1) |
||
| 325 | { |
||
| 326 | var hexCode=idx.toString(16); |
||
| 327 | resultsPage = this.resultsPath + '/' + indexSectionNames[this.searchIndex] + '_' + hexCode + '.html'; |
||
| 328 | resultsPageWithSearch = resultsPage+'?'+escape(searchValue); |
||
| 329 | hasResultsPage = true; |
||
| 330 | } |
||
| 331 | else // nothing available for this search term |
||
| 332 | { |
||
| 333 | resultsPage = this.resultsPath + '/nomatches.html'; |
||
| 334 | resultsPageWithSearch = resultsPage; |
||
| 335 | hasResultsPage = false; |
||
| 336 | } |
||
| 337 | |||
| 338 | window.frames.MSearchResults.location = resultsPageWithSearch; |
||
| 339 | var domPopupSearchResultsWindow = this.DOMPopupSearchResultsWindow(); |
||
| 340 | |||
| 341 | if (domPopupSearchResultsWindow.style.display!='block') |
||
| 342 | { |
||
| 343 | var domSearchBox = this.DOMSearchBox(); |
||
| 344 | this.DOMSearchClose().style.display = 'inline'; |
||
| 345 | if (this.insideFrame) |
||
| 346 | { |
||
| 347 | var domPopupSearchResults = this.DOMPopupSearchResults(); |
||
| 348 | domPopupSearchResultsWindow.style.position = 'relative'; |
||
| 349 | domPopupSearchResultsWindow.style.display = 'block'; |
||
| 350 | var width = document.body.clientWidth - 8; // the -8 is for IE :-( |
||
| 351 | domPopupSearchResultsWindow.style.width = width + 'px'; |
||
| 352 | domPopupSearchResults.style.width = width + 'px'; |
||
| 353 | } |
||
| 354 | else |
||
| 355 | { |
||
| 356 | var domPopupSearchResults = this.DOMPopupSearchResults(); |
||
| 357 | var left = getXPos(domSearchBox) + 150; // domSearchBox.offsetWidth; |
||
| 358 | var top = getYPos(domSearchBox) + 20; // domSearchBox.offsetHeight + 1; |
||
| 359 | domPopupSearchResultsWindow.style.display = 'block'; |
||
| 360 | left -= domPopupSearchResults.offsetWidth; |
||
| 361 | domPopupSearchResultsWindow.style.top = top + 'px'; |
||
| 362 | domPopupSearchResultsWindow.style.left = left + 'px'; |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | this.lastSearchValue = searchValue; |
||
| 367 | this.lastResultsPage = resultsPage; |
||
| 368 | }; |
||
| 369 | |||
| 370 | // -------- Activation Functions |
||
| 371 | |||
| 372 | // Activates or deactivates the search panel, resetting things to |
||
| 373 | // their default values if necessary. |
||
| 374 | this.Activate = function(isActive) |
||
| 375 | { |
||
| 376 | if (isActive || // open it |
||
| 377 | this.DOMPopupSearchResultsWindow().style.display == 'block' |
||
| 378 | ) |
||
| 379 | { |
||
| 380 | this.DOMSearchBox().className = 'MSearchBoxActive'; |
||
| 381 | |||
| 382 | var searchField = this.DOMSearchField(); |
||
| 383 | |||
| 384 | if (searchField.value == this.searchLabel) // clear "Search" term upon entry |
||
| 385 | { |
||
| 386 | searchField.value = ''; |
||
| 387 | this.searchActive = true; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | else if (!isActive) // directly remove the panel |
||
| 391 | { |
||
| 392 | this.DOMSearchBox().className = 'MSearchBoxInactive'; |
||
| 393 | this.DOMSearchField().value = this.searchLabel; |
||
| 394 | this.searchActive = false; |
||
| 395 | this.lastSearchValue = ''; |
||
| 396 | this.lastResultsPage = ''; |
||
| 397 | } |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 792 |